/*
 * @Author: LIFM0623 1944099171@qq.com
 * @Date: 2024-01-22 15:23:12
 * @LastEditors: EyeSklear 1814580085@qq.com
 * @LastEditTime: 2024-03-27 15:17:40
 * @FilePath: \Scene_Back\src\map-ease\map-ease.service.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { Injectable, NotFoundException } from '@nestjs/common';
import { mapEase } from './entities/map-ease.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, In } from 'typeorm';
import { createMapEaseDto } from './dto/create-mapEase.dto';
import { updateMapEaseDto } from './dto/update-mapEase.dto';

@Injectable()
export class MapEaseService {
  constructor(
    @InjectRepository(mapEase)
    private readonly mapEaseRepository: Repository<mapEase>,
  ) {}

  async remove(id: string) {
    const removeItem = await this.mapEaseRepository.delete({ id: id });
    if (removeItem) {
      return '删除成功!  ';
    } else {
      return '删除失败!';
    }
  }
  async update(id: string, updateMpaEaseDto: updateMapEaseDto) {
    const result = await this.mapEaseRepository.update(id, updateMpaEaseDto);

    // 检查更新操作是否影响了任何行
    if (result.affected === 0) {
      throw new NotFoundException(`Picture with ID "${id}" not found.`);
    }
    return '修改视角转移属性成功';
  }
  async findOne(id: string): Promise<mapEase> {
    return this.mapEaseRepository.findOne({ where: { id: id } });
  }

  async findByIds(ids: string[]): Promise<mapEase[]> {
    return this.mapEaseRepository.find({
      where: {
        id: In(ids), // 使用TypeORM的In操作符
      },
    });
  }

  async findUser(creator: string): Promise<mapEase[]> {
    return this.mapEaseRepository.find({ where: { creator: creator } });
  }
  async findAll() {
    return this.mapEaseRepository.find();
  }
  async createMapEaseItem(creatMapEaseDto: createMapEaseDto) {
    const insertItem = this.mapEaseRepository.create(creatMapEaseDto);
    const insertRes = await this.mapEaseRepository.insert(insertItem);
    return insertRes.raw;
  }

  async createMapEaseItemForScript(creatMapEaseDto: createMapEaseDto) {
    const insertItem = this.mapEaseRepository.create(creatMapEaseDto);
    const insertRes = await this.mapEaseRepository.insert(insertItem);
    return insertRes.raw[0].id;
  }
}
